/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} x
 * @return {ListNode}
 */

function ListNode(val, next) {
  this.val = (val === undefined ? 0 : val)
  this.next = (next === undefined ? null : next)
}
var partition = function (head, x) {
  let dummyHead = new ListNode()
  dummyHead.next = head
  if (head) {
    let newList = new ListNode
    let p = newList
    let pre = dummyHead
    let cur = head
    while (cur) {
      if (cur.val >= x) {
        p.next = cur
        pre.next = cur.next
        cur = cur.next
        p = p.next
      } else {
        pre = pre.next
        cur = pre.next
      }
    }
    p.next = null
    pre.next = newList.next
  }
  return dummyHead.next
};

let l = {
  val: 1,
  next: {
    val: 4,
    next: {
      val: 3,
      next: {
        val: 2,
        next: {
          val: 5,
          next: {
            val: 2,
            next: null
          }
        }
      }
    }
  }
}



console.log(partition(l, 3));